﻿@COFE ?= {}

$ = jQuery
Entry = COFE.Entry
EntryItem = COFE.EntryItem
EntryItemListBase = COFE.EntryItemListBase
EntryMeta = COFE.EntryMeta
EntryMetaItem = COFE.EntryMetaItem
utils = COFE.utils

class EntryDetailList extends EntryItemListBase
  constructor : () ->
    super
    @childTemplateKey = "#metadataTemplate"
    @templateKey = "#entryDetailTemplate"
    
  elements : 
    "#entryInfo" : "entryInfo"    
    "#metadata" : "metadata"    
    ".header .headerCaption" : "header"
    ".back" : "btnBack"
    ".next" : "btnNext"
  
  init : =>
    @active = false
    #When entry list updated...
    @bind("updated", =>         
      $.mobile.hidePageLoadingMsg()  
      if @App.selection isnt undefined  
      then @changeSelection(@App.selection)
      else @changeSelection(@entryItems[0].entry)
      )

    @App.bind("selectionChanged", @onSelectionChanged)   
    @App.bind("pathChanged", @onPathChanged) 

    @btnBack.bind("click", @back)
    @btnNext.bind("click", @next)      
        
    @el.live("pageshow", @activate)
    @el.live("pagehide", @deactivate)
  
  updateBtnState : =>
     if @cangonext() then @btnNext.show() else @btnNext.hide()
     if @cangoback() then @btnBack.show() else @btnBack.hide()

  activate : =>
    @active = true        
    @changePath(@App.path)

  deactivate : =>
    @active = false

  cangonext : =>
    1 + @selectedIndex < @itemCount  

  cangoback : =>
    @selectedIndex > 0 

  next : =>      
    if @cangonext() 
      @App.changeSelection(@entryItems[@selectedIndex+1].entry)

  back : =>        
    if @cangoback() 
      @App.changeSelection(@entryItems[@selectedIndex-1].entry)

  onPathChanged : (sender, path) =>
    if @active
      super(sender, path)

  onSelectionChanged : (sender, selection) =>
    if @active
      @changeSelection(selection)


  changePath : (path) =>
    $.mobile.showPageLoadingMsg()    
    @header.text(path.header)
    super(path)

  changeSelection : (selection) =>    
    if @active
      #Continue if selection changed.
      if selection isnt undefined and selection isnt @selection
        $.mobile.showPageLoadingMsg()
       
        @selection = selection
        @selectedIndex = @indexOf(selection)
        @itemCount = @entryItems.length
        @updateBtnState()
         
        #Unbind events
        if @metadata isnt undefined
          @metadata.unbind("fetched")
          @metadata.unbind("error")
      
        el = $("<ul>")
        el.attr("data-role", "listview")
        el.attr("data-inset", "true")

        @metaItem = EntryMetaItem.init { entry : selection, templateKey : @childTemplateKey, el : el  }
        @metaItem.bind("fetched", @render)
        @metaItem.bind("error", (s, err) =>
          $.mobile.hidePageLoadingMsg()
          @App.showMessage(err, "Error and retrieving metadata.")
        )
        @metaItem.fetch()

  template : =>
    $(@templateKey).tmpl({ 
      selectedIndex : @selectedIndex + 1
      itemCount : @itemCount 
      entry : @selection
    })        

  render : =>    
    $.mobile.hidePageLoadingMsg()
    @entryInfo.html(@template())
    #console.log $(@entryInfo, ".listview")
    
    @metadata.html(@metaItem.render())
    @el.trigger("create")

COFE.EntryDetailList = EntryDetailList